package group_1.MultiSnake;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import client_server.IO.HandleServerData;
import client_server.IO.NetworkIO;

public class PlayGame extends ListActivity implements OnClickListener, HandleServerData {
	private static String HOST_GAME_ACTION = "host_game";
	private static String GET_LIST_ACTION = "get_list";
	
	private static int JOIN_GAME_ID = 0;
	private static int HOST_GAME = 1;
	
	private Button createGameBtn;
	private ListView gameView;
	private NetworkIO networkIO;
	private Handler guiThread;
	
	private Context context;
	
	private String[] games;
	private String selectedGame;
	
	
	//private List<Game> gameList = new ArrayList<Game>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list);
		networkIO = new NetworkIO(this);
		guiThread = new Handler();
		context = this.getApplicationContext();
		
		gameView = (ListView) this.findViewById(android.R.id.list);
		createGameBtn = (Button) findViewById(R.id.create_game);

		setListeners();
		
		
		
		  gameView.setTextFilterEnabled(true);
		  gameView.setOnItemClickListener(new OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View view,
		        int position, long id) {
		    	selectedGame = games[position];
		    	startActivityForResult(new Intent(context, JoinGamePop.class), JOIN_GAME_ID);
//		    	startActivity(new Intent(getApplicationContext(), JoinGamePop.class));
		    	}
		  });
		  
		  networkIO.getGameList();
	}
	
	public void onClick(View v) {
		switch(v.getId()){
		
		case R.id.create_game:
			startActivityForResult(new Intent(this, CreateGame.class), HOST_GAME);
			break;
		}
		
		
	}
	
	
    private void setListeners() {
    	createGameBtn.setOnClickListener(this);
    }
    
    private void renderGameList(final String data){
    	guiThread.post(new Runnable() {
    		public void run() {
    			games = getGames(data);
    			if (!(games.length == 0)){ 
    				gameView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, games));
    				//gameView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,games));
    			}
    			else
    				gameView.setAdapter(new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, new String[]{"No games found"}));
    		}
    	});
    }
    
    private String[] getGames(String data){
    	//TODO: Get list of games from server.
    	data = data.substring(data.indexOf(';')+1, data.length());
//    	String[] dataStr;
//    	dataStr = data.split(":");
//    	String[] games = new String[dataStr.length-1];
//    	for(int i = 0; i < dataStr.length; i++) {
//    		games[i-1] = dataStr[i];
//    	}
    	return data.split(":");
    }
    
	@Override
	public void handleServerData(String data) {
		String[] dataStr = data.split(";");
		
		if(dataStr[0].equals(HOST_GAME_ACTION)) {  // Format "action;gameId;gameName;nrOfJoined;maxPlayers"
			selectedGame = data.substring(data.indexOf(';')+1, data.length());
			newGame();
		} else if(dataStr[0].equals(GET_LIST_ACTION)) {
			renderGameList(data);
		}
	}
	
	private void newGame() {
		Intent intent = new Intent(this, GameActivity.class);
		intent.putExtra(getString(R.string.game_key), selectedGame);
		startActivity(intent);
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if(resultCode == Activity.RESULT_OK) {
	    	
	    	if(requestCode == JOIN_GAME_ID) {
	    		newGame();
	    	} else if(requestCode == HOST_GAME) {
	    		String gameName = data.getStringExtra(getString(R.string.game_key));
	    		networkIO.hostGame(gameName);
	    	}
		}
	}
	
	@Override
	public void onPause() {
		super.onPause();
		if(networkIO != null) {
			networkIO.closeConnections();
		}
	}
	
//	@Override
//	public void onResume() {
//		super.onResume();
//		if(networkIO != null) {
//			networkIO.getGameList();
//		}
//	}
}